import 'dart:io';

import 'package:environmental_les_app/common/routes/route_pages.dart';
import 'package:flutter/services.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import '../common/l10n/translations.dart';
import '../common/net/api_config.dart';
import '../common/net/net.dart';
import '../common/routes/route_names.dart';
import '../common/themes/themes.dart';
import '../common/utils/sp_util.dart';

/// 启动应用
class StartApp extends StatelessWidget {
  const StartApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        KeyBoardUtils.hideKeyboard();
      },
      child: GetMaterialApp(
        title: '环境监测执法',
        theme: Themes.main,
        getPages: RoutePages.routePage,
        initialRoute: getToken().isNullOrEmpty()
            ? AppRoutes.loginPage
            : AppRoutes.mainPage,
        builder: EasyLoading.init(),
        translations: AppTranslations(),
        supportedLocales: AppTranslations.supportedLocales,
        locale: Get.deviceLocale,
        fallbackLocale: AppTranslations.fallbackLocale,
        localizationsDelegates: const [
          GlobalCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
      ),
    );
  }
}

/// 初始化配置
Future<void> initConfig(Env env) async {
  ApiConfig.env = env; // 配置环境
  if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle =
        const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }

  WidgetsFlutterBinding.ensureInitialized();
  await SpUtil.getInstance();
  DioUtils.instance.initConfig(); // 初始化网络请求框架
}
